package com.sdy.resdir.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sdy.auth.api.model.UserInfo;
import com.sdy.auth.client.service.SsoService;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.resdir.biz.model.RdResCatalogTableInfo;
import com.sdy.resdir.biz.model.RdResourceCataloging;
import com.sdy.resdir.biz.service.RdResCatalogTableInfoService;
import com.sdy.resdir.biz.service.RdResourceCatalogingService;
import com.sdy.common.model.Response;
import com.sdy.resdir.biz.util.DeCodeUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资源编目 前端控制器
 * </p>
 *
 * @author hyh
 * @since 2019-10-21
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@Api(tags = "资源编目 -- 目前弃用")
@RequestMapping("/rdResourceCataloging")
public class RdResourceCatalogingController extends BaseController {
    @Autowired
    private RdResourceCatalogingService resCatalogService;
    @Autowired
    private SsoService ssoService;
    @Autowired
    private RdResCatalogTableInfoService tableInfoService;


    @ApiOperation(value = "资源编目分页查询 -- 更新时间这个字段还不确定类型,暂时不使用 -- 目前弃用", response = RdResourceCataloging.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "release", value = "是否发布:1-已发布   2-未发布"),
            @ApiImplicitParam(name = "resName", value = "资源名称,模糊查询"),
            @ApiImplicitParam(name = "deptId", value = "所属部门id"),
            @ApiImplicitParam(name = "shareType", value = "共享方式(1.受限2.公开)"),
            @ApiImplicitParam(name = "update", value = "更新频率"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")}
    )
    @GetMapping("/getResLogPageList")
    public Response getResLogPageList(Page page, Integer release, String resName, Integer deptId, Integer shareType, String update, String startTime, String endTime) throws Exception {
        // TODO 更新时间这个字段还不确定类型,暂时不使用
        resName = DeCodeUtil.decodeParam(resName);
        startTime = DeCodeUtil.decodeParam(startTime);
        endTime = DeCodeUtil.decodeParam(endTime);
        IPage data = resCatalogService.page(page, Wrappers.<RdResourceCataloging>lambdaQuery()
                .orderByDesc(RdResourceCataloging::getCatalogingTime)
                .eq(null != release, RdResourceCataloging::getIsRelease, release)
                .like(StringUtil.isNotBlank(resName) && StringUtil.isNotEmpty(resName), RdResourceCataloging::getResName, resName)
                .eq(null != deptId, RdResourceCataloging::getDeptId, deptId)
                .eq(null != shareType, RdResourceCataloging::getShareType, shareType)
                .between(StringUtil.isNotBlank(startTime) && StringUtil.isNotEmpty(startTime), RdResourceCataloging::getCatalogingTime, startTime, endTime)
        );
        return Response.success(data);
    }


    @ApiOperation(value = "新增或者编辑资源编目 -- 目前弃用")
    @PostMapping("/saveOrUpdateResLog")
    public Response saveOrUpdateResLog(@RequestBody @ApiParam(name = "cataloging", value = "资源编目对象") RdResourceCataloging cataloging, HttpServletRequest request) throws Exception {
        UserInfo userInfo = ssoService.getUserInfo(request);
        Assert.isNull(userInfo, "用户未登录");
//        List<Integer> roleIdList = userQueryApi.getUser(userInfo.getUserId()).getRoleIdList();
//        boolean isDel = true;
//        for (Integer roleId : roleIdList) {
//            if (1 == roleId) {
//                isDel = false;
//            }
//        }
//        Assert.isTrue(!isDel, "没有权限进行该操作");
        int i = resCatalogService.saveOrUpdateResLog(cataloging, userInfo);
        return i == 1 ? Response.success() : Response.error();
    }

}
